<script type="text/javascript">
$(function(){
	var pg = App.regionPicker.ui;
	pg.params = pg.params || { keys: [] };
	var keys = pg.params.keys || [];
	var rcs = App.logon.roles;
	var tt = Msg.login.code;
	//由于contentMenu.js中计算menu的位置有问题，所以将ctxMenu放到了网站首页index.html中
	var ctxMenu = $('#rgctxMenu').css('z-index','2000');	
	var tree = pg.find('.tree');
	var tp = tree.position();
	tree.mac('dtree', {
		treeConfig: {
			title: Msg.region.allRegions,
			rootVisible: true,
			checkbox: pg.params.multi,
			selectMode: 2,
			onDblClick: function(dt, event) {
				dt.toggleSelect();
			},
			onSelect: function(f, dt){
				var k = dt.data.key;
				var i = $.inArray(k, keys);
				if(f && i<0){
					keys.push(k);
				}else if(!f && i>=0){
					keys.splice(i, 1);
				}
			},
			onKeydown: function(dt, e) {
				if(e.which == 32 ) {
					dt.toggleSelect();
					return false;
				}
			}
		},
		ctxMenu: ctxMenu,
		menuOffset: true,
		doInsert: function(dt, callback){
			var d = dt.data;
			d.level = d.level?d.level:0;
			var p = { level: 1, path: '/', parent: 'root' };
			if(d.level>0){
				p.parent = d.key;
				p.level = d.level + 1;
				if(d.level==1){
					p.path = '/'+ d.key + '/';
				}else{
					p.path = d.path + d.key + '/';
				}
				p.value = d.value;
			}
			App.open({
				action: 'editRegion',
				title: Msg.region.add,
				modal: true,
				params: p
			}, function(dd){
				callback(dd);
				//dt.deactivate();
				//dt.activate();
			});
		},
		doUpdate: function(dt, callback){
			var d = dt.data;
			App.open({
				action: 'editRegion',
				title: Msg.region.add,
				modal: true,
				params: d
			}, function(dd){
				callback(dd);
			});
		},
		doDelete: function(dt, callback){
			App.confirm(Msg.confirm.message, function(){
				var p = { id: dt.data.key, level: dt.data.level };
				$.post('region/delete.do', p, function(data){
					var ro = App.eval(data);
					if(ro.success){
						App.alert(Msg.info.success, [ Msg.del ]);
						callback();
						var tx = tree.getNode(dt.data.parent);
						tx.activate();
					}else{
						App.alert(ro.data, ro.params);
					}
				});
			}, [ Msg.del ]);
		},
		loader: {
			url: 'region/list.do',
			params: { key: 'root' },
			autoLoad: true
		},
		onCtxMenu: function(el){
			var d = el.prop('dtnode').data;
			//ctxMenu.enableContextMenuItems();
			ctxMenu.disableContextMenuItems('insert,update,delete');
			if($.inArray('reportAdmin', rcs)>=0 || $.inArray('admin', rcs)>=0){
				ctxMenu.enableContextMenuItems();
				if(!d.level){
					ctxMenu.disableContextMenuItems('update,delete');
				}
			}						
		},
		onLoadSubTree: function(dd){
			$.each(dd, function(n, c){
				if($.inArray(c.key, keys)>=0)
					tree.getNode(c.key).select(true);
			});
		}
	});
	pg.seek('cancel').click(function() {
		pg.dialog("close");
	}).button();
	pg.seek('ok').click(function() {
		var dd = pg.params.data || {};
		var nd = {};
   		$.each(keys, function(n,c){
   			if(dd[c]){
   				nd[c] = dd[c];
   			}else{
  				var d = tree.getNode(c).data;
  				nd[c] = { region: d, weight: 0 };
   			}
       	});
		pg.dialog("close");
		pg.callback.call(self, nd);
	}).button();
	App.setLanguage();
});
</script>
<ul id="rgctxMenu" name="rgctxMenu" class="contextMenu">
	<li action="insert" class="add item ui-helper-clearfix">
		<span style="display: block">
			<span class="icon ui-icon ui-icon-plus"></span>
			<span class="text">Msg.add</span>
		</span>
	</li>
	<li action="update" class="edit item ui-helper-clearfix">
		<span style="display: block">
			<span class="icon ui-icon ui-icon-pencil"></span>
			<span class="text">Msg.edit</span>
		</span>
	</li>
	<li action="delete" class="delete item ui-helper-clearfix">
		<span style="display: block">
			<span class="icon ui-icon ui-icon-minus"></span>
			<span class="text">Msg.del</span>
		</span>
	</li>
</ul>
<div class="tree" style="overflow: auto; height: 360px; border: 1px solid #CCCCCC"></div>
<div class="clear" style="height: 8px;"></div>
<div style="text-align: center">
	<button name="ok"><span class="text">Msg.ok</span></button>&nbsp;
	<button name="cancel"><span class="text">Msg.cancel</span></button>
</div>